package com.situ.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.mall.pojo.dto.LoginInfoDTO;
import com.situ.mall.pojo.dto.RegisterInfoDTO;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.service.impl.UserServiceImpl;
import com.situ.mall.util.JwtUtil;
import com.situ.mall.util.Result;
import com.situ.mall.util.SHA256Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserServiceImpl userService;

    @PostMapping("/register")
    public Result register(@RequestBody RegisterInfoDTO registerInfoDTO) {
        //首先判断验证码是否正确
        String captcha = (String) redisTemplate.opsForValue().get("captcha:" + registerInfoDTO.getUuid());
        if (ObjectUtils.isEmpty(captcha)) {
            return Result.error("验证码已经过期");
        }
        if (!captcha.equalsIgnoreCase(registerInfoDTO.getCaptcha())) {
            return Result.error("验证码错误");
        }

        //名字是唯一的
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", registerInfoDTO.getName());
        User loginUser = userService.getOne(queryWrapper);
        if (loginUser != null) {
            return Result.error("用户名已经重名，请更换一个");
        }

        User user = new User();
        BeanUtils.copyProperties(registerInfoDTO, user);
        user.setPassword(SHA256Util.encrypt(registerInfoDTO.getPassword()));
        userService.save(user);
        return Result.ok("注册成功");
    }

    @PostMapping("/login")
    public Result login(@RequestBody LoginInfoDTO loginInfoDTO) {
        //首先判断验证码是否正确
        String captcha = (String) redisTemplate.opsForValue().get("captcha:" + loginInfoDTO.getUuid());
        if (ObjectUtils.isEmpty(captcha)) {
            return Result.error("验证码已经过期");
        }
        if (!captcha.equalsIgnoreCase(loginInfoDTO.getCaptcha())) {
            return Result.error("验证码错误");
        }

        //名字是唯一的
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", loginInfoDTO.getName());
        User loginUser = userService.getOne(queryWrapper);
        if (loginUser == null) {
            return Result.error("用户名错误");
        }

        //判断密码是否正确
        /*if (!loginInfoDTO.getPassword().equals(loginUser.getPassword())) {
            return Result.error("密码错误");
        }*/
        if (!SHA256Util.encrypt(loginInfoDTO.getPassword()).equals(loginUser.getPassword())) {
            return Result.error("密码错误");
        }

        //是否禁用
        if (loginUser.getStatus() == 0) {
            return Result.error("该用户已经被禁用");
        }

        //登录成功
        Map<String, Object> map = new HashMap<>();
        map.put("id", loginUser.getId());
        map.put("name", loginUser.getName());
        String token = JwtUtil.createToken(map);
        return Result.ok("登录成功", token);
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo(@RequestHeader(name = "Authorization")String token) {
        Map<String, Object> tokenMap = JwtUtil.parseToken(token);
        Integer id = (Integer) tokenMap.get("id");
        User user = userService.getById(id);
        user.setPassword(null);
        return Result.ok(user);
    }
}

